﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_QuanLyHoSo_Detail
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Event for control "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Certifiedid").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Certifiedid")
                hidCurHuyenId.Value = Session("HuyenId")

                Load2Grid()
                Load3Grid()
                BindToGrid()
            End If
        End If
    End Sub
    Protected Sub Load2Grid()
        Using _data As New VSEntities

            Dim p = (From q In _data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Select q).FirstOrDefault
            If p Is Nothing Then
                Excute_Javascript("Alertbox('Có lỗi vui lòng kiềm tra đường truyền !.');", Me.Page, True)
                Exit Sub
            End If

            'số GCN và ngày cấp
            txtSoGCN.Text = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode)
            If Not p.CertifiedDate Is Nothing AndAlso p.CertifiedDate <> "01/01/1900" Then
                txtNgayCapGCN.Text = CType(p.CertifiedDate, Date).ToString("dd/MM/yyyy") 'Ngày cấp
            End If
            'Thông tin người đại diện
            lblOwRepresentative.Text = IIf(IsNothing(p.Ow_Name) = True, "", p.Ow_Name) 'Họ tên
            lblOwCardId.Text = IIf(IsNothing(p.Ow_CardID) = True, "", p.Ow_CardID) 'Số CMND
            If Not p.Ow_CardDate Is Nothing Then
                lblOwCardDate.Text = CType(p.Ow_CardDate, Date).ToString("dd/MM/yyyy") 'Ngày cấp
            End If
            'thông tin hô kinh doanh
            lblBizName.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName) 'Tên hộ kinh doanh
            lblCertifiedCode.Text = IIf(IsNothing(p.CertifiedCode) = True, "", p.CertifiedCode) 'Số GCN
            lblOw_Tel.Text = IIf(IsNothing(p.Ow_Tel) = True, "", p.Ow_Tel) 'điện thoai
            lblBiz_HeadOffice.Text = IIf(IsNothing(p.Biz_HeadOffice) = True, "", p.Biz_HeadOffice) 'địa điểm kinh doanh

        End Using
    End Sub
    Protected Sub Load3Grid()
        Using _data As New VSEntities
            'provinces 
            Dim _tinhthanh = (From q In _data.Provinces Where q.ParentId = 0 Order By q.Title Select q.Id, q.Code, q.Title).ToList

            With ddlTinhDiaDiemKD
                .DataSource = _tinhthanh
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataBind()
                .Items.Add(New ListItem("--- Chọn ---", 0))
                .SelectedValue = 0
            End With

            With ddlTinhHoKhau
                .DataSource = _tinhthanh
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataBind()
                .Items.Add(New ListItem("--- Chọn ---", 0))
                .SelectedValue = 0
            End With

            ''tam tru

            With ddlTinhHienTai
                .DataSource = _tinhthanh
                .DataValueField = "Id"
                .DataTextField = "Title"
                .DataBind()
                .Items.Add(New ListItem("--- Chọn ---", 0))
                .SelectedValue = 0
            End With


            'NGanh nghe
            Dim linhvuc = (From q In _data.BusinessFields Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlBusinessField
                .DataSource = linhvuc
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With

            ''dan toc
            Dim dantoc = (From q In _data.Citizens Order By q.Name Select New With {.Id = q.Name, .Name = q.Name}).ToList
            With ddlCitizen
                .DataSource = dantoc
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With

            'quoc gia
            Dim Country = (From q In _data.Countries Order By q.Name Select New With {.Id = q.Name, .Name = q.Name}).ToList
            With ddlCountry
                .DataSource = Country
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With

            'dong vi cap giay chung nhan CMND
            Dim DeptCertified = (From q In _data.DeptIdentifies Order By q.Name Select New With {.Id = q.Name, .Name = q.Name}).ToList
            With ddlDeptCertified
                .DataSource = DeptCertified
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With

            ' 'don vi cap giay khac
            Dim d = (From q In _data.DeptIdentifies Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlOthersPaper_NoiCap
                .DataSource = d
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With

            'giấy tờ khác
            Dim CertifiedPaperDict = (From q In _data.CertifiedPaperDicts Order By q.Name Select New With {.Id = q.Id, .Name = q.Name}).ToList
            With ddlOthersPaper
                .DataSource = CertifiedPaperDict
                .DataValueField = "Id"
                .DataTextField = "Name"
                .DataBind()
                .Items.Insert(0, New ListItem("--- Chọn ---", "0"))
            End With
            ''thanh phan ho so
            Dim lstThanhPhanHoSo = (From q In _data.ThanhPhanHoSoes Order By q.IsDefault Descending, q.TenHoSo Select q).ToList
            Dim strKey_Name() As String = {"Id", "MaHoSo", "IsDefault"}
            With grdShowTp
                .DataKeyNames = strKey_Name
                .DataSource = lstThanhPhanHoSo
                .DataBind()
            End With

            Dim p = (From q In _data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Select q).FirstOrDefault

            If p Is Nothing Then
                Excute_Javascript("Alertbox('Có lỗi vui lòng kiểm tra đường truyền !.');", Me.Page, True)
                Exit Sub
            End If
            'thong tin ca nhan
            txtOwRepresentative.Text = IIf(IsNothing(p.Ow_Name) = True, "", p.Ow_Name) 'Họ tên
            rdlSex.SelectedValue = IIf(IsNothing(p.Ow_Sex) = True, "", p.Ow_Sex) 'giới tính

            'ngay sinh
            Dim _ngaysinh = Len(p.Ow_BirthDate)
            Dim _strNgaySinh As String = Replace(p.Ow_BirthDate, "/", "")
            If Not p.Ow_BirthDate Is Nothing AndAlso p.Ow_BirthDate <> "01/01/1900" Then
                If IsDate(p.Ow_BirthDate) Then
                    txtOwDateOfBirth.Text = CType(p.Ow_BirthDate, Date).ToString("dd/MM/yyyy") 'ngày tháng năm sinh
                Else
                    If _ngaysinh >= 8 Then
                        Dim _nam As String = Mid(_strNgaySinh, 5, 8)
                        Dim _thang As String = Mid(_strNgaySinh, 3, 2)
                        Dim _ngay As String = Mid(_strNgaySinh, 1, 2)
                        If (Int32.Parse(_thang) = 0) And (Int32.Parse(_ngay) = 0) Then
                            txtOwDateOfBirth.Text = _nam 'ngày tháng năm sinh
                        Else
                            txtOwDateOfBirth.Text = _ngay & "/" & _thang & "/" & _nam 'ngày tháng năm sinh
                        End If
                    ElseIf _ngaysinh = 4 Then
                        txtOwDateOfBirth.Text = _strNgaySinh 'ngày tháng năm sinh
                    End If
                End If
            End If
            'dân tộc
            'trường hợp dữ liệu inport vào trường dân tộc là chuỗi
            Dim National = p.Ow_National_ID
            If IsNumeric(National) Then 'kiểm tra number hay string
                If Not p.Ow_National_ID Is Nothing AndAlso p.Ow_National_ID <> "" Then
                    ddlCitizen.SelectedValue = p.Ow_National_ID
                End If

            Else
                Dim _joinNational = (From k In _data.Citizens Where k.Name = National Select k).FirstOrDefault
                If Not _joinNational Is Nothing Then
                    ddlCitizen.SelectedValue = _joinNational.Name
                End If

            End If

            'Quốc tịch
            'trường hợp dữ liệu inport vào trường Quốc tịch là chuỗi
            Dim Nationality = p.Ow_Nationality_ID
            If IsNumeric(Nationality) Then 'kiểm tra number hay string
                If Not p.Ow_Nationality_ID Is Nothing AndAlso p.Ow_Nationality_ID <> "" Then
                    ddlCountry.SelectedValue = p.Ow_Nationality_ID
                End If
            Else
                Dim _joinNationality = (From k In _data.Countries Where k.Name = Nationality Select k).FirstOrDefault
                If Not _joinNationality Is Nothing Then
                    ddlCountry.SelectedValue = _joinNationality.Name
                End If

            End If

            txtOwCardId.Text = IIf(IsNothing(p.Ow_CardID) = True, "", p.Ow_CardID) 'Số CMND
            If Not p.Ow_CardDate Is Nothing Then
                txtOwCardDate.Text = CType(p.Ow_CardDate, Date).ToString("dd/MM/yyyy") 'Ngày cấp
            End If
            'Nơi cấp
            'Quốc tịch
            'trường hợp dữ liệu inport là chuỗi
            Dim DeptId = p.Ow_Provider_ID
            If IsNumeric(DeptId) Then 'kiểm tra number hay string
                If Not p.Ow_Provider_ID Is Nothing AndAlso p.Ow_Provider_ID <> "" Then
                    ddlDeptCertified.SelectedValue = p.Ow_Provider_ID
                End If
            Else
                Dim _joinDeptCertified = (From k In _data.DeptIdentifies Where k.Name = DeptId Select k).FirstOrDefault
                If Not _joinDeptCertified Is Nothing Then
                    ddlDeptCertified.SelectedValue = _joinDeptCertified.Name
                End If

            End If
            'giay to khac
            If Not p.Ow_Doc_ID Is Nothing AndAlso p.Ow_Doc_ID <> "" Then
                ddlOthersPaper.SelectedValue = p.Ow_Doc_ID 'Giấy tờ khác
            Else
                ddlOthersPaper.Items.Clear()

            End If

            txtOtherPaperCode.Text = IIf(IsNothing(p.Ow_DocCode) = True, "", p.Ow_DocCode) 'Mã số

            If Not p.Ow_DocDate Is Nothing AndAlso p.Ow_DocDate <> "01/01/1900" Then
                txtOthersPaperDateIssue.Text = CType(p.Ow_DocDate, Date).ToString("dd/MM/yyyy") 'Ngày cấp
            End If

            If Not p.Ow_DocOffiCertifiedID Is Nothing AndAlso p.Ow_DocOffiCertifiedID <> "" Then
                ddlOthersPaper_NoiCap.SelectedValue = p.Ow_DocOffiCertifiedID 'Nơi cấp
            Else
                ddlOthersPaper_NoiCap.Items.Clear()
            End If


            ''Thông tin hộ kinh doanh: 
            txtBizName.Text = IIf(IsNothing(p.Biz_VietName) = True, "", p.Biz_VietName) 'Tên hộ kinh doanh
            txtOw_Tel.Text = IIf(IsNothing(p.Ow_Tel) = True, "", p.Ow_Tel) 'điện thoai
            txtOw_Fax.Text = IIf(IsNothing(p.Ow_Fax) = True, "", p.Ow_Fax) 'fax
            txtOw_Email.Text = IIf(IsNothing(p.Ow_Email) = True, "", p.Ow_Email) 'Email
            txtOw_WebSite.Text = IIf(IsNothing(p.Ow_WebSite) = True, "", p.Ow_WebSite) 'Website
            'nganh nghe kinh doanh và linh vưc
            ' p.NganhNgheKinhDoanh = ddlNganhNgheKinhDoanh.SelectedValue 'ngành nghề kinh doanh
            If Not p.LinhVucKinhDoanh Is Nothing AndAlso p.LinhVucKinhDoanh <> "" Then
                ddlBusinessField.SelectedValue = p.LinhVucKinhDoanh 'Lĩnh vực kinh doanh
            End If


            txtVonTienMat.Text = IIf(IsNothing(p.VonTienMat) = True, "", p.VonTienMat) 'Von tien mat
            txtVonTaiSan.Text = IIf(IsNothing(p.VonTaiSan) = True, "", p.VonTaiSan) 'Von tai san
            If Not p.Biz_InvestCapital Is Nothing Then
                txtBiz_InvestCapital.Text = FormatCurrency(p.Biz_InvestCapital).Replace(",", ".")  'vốn kinh doanh
            End If


            txtBiz_WordsMoney.Text = IIf(IsNothing(p.Biz_WordsMoney) = True, "", p.Biz_WordsMoney) 'Tiền viết bằng chữ
            txtMoney_string.Text = IIf(IsNothing(p.Biz_WordsMoney) = True, "", p.Biz_WordsMoney)

            lblAddressThuongTru.Text = IIf(IsNothing(p.Ow_Resident) = True, "", p.Ow_Resident) 'địa chỉ thường trú
            lblAddressTamTru.Text = IIf(IsNothing(p.Ow_Present) = True, "", p.Ow_Present) 'địa chỉ tạm trú
            lblAddressKinhDoanh.Text = IIf(IsNothing(p.Biz_HeadOffice) = True, "", p.Biz_HeadOffice) 'địa chỉ kinh doanh
            'Địa chỉ phường xa huyên
            'thuong trú
            Dim _jth = (From q In _data.CertifiedHouseholdAddresses Where q.CertifiedID = hidID.Value And q.TypeId = LoaiDiaChi.ThuongTru Select q).FirstOrDefault
            If Not _jth Is Nothing Then

                txtDiaChiHoKhau.Text = _jth.Adress
                ddlTinhHoKhau.SelectedValue = _jth.TinhId
                Dim _Huyen = (From q In _data.Provinces Where q.ParentId = _jth.TinhId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlHuyenHoKhau
                    .DataSource = _Huyen
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlHuyenHoKhau.SelectedValue = _jth.HuyenId
                Dim _PhuongXa = (From q In _data.Provinces Where q.ParentId = _jth.HuyenId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlXaThiTranHoKhau
                    .DataSource = _PhuongXa
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlXaThiTranHoKhau.SelectedValue = _jth.XaThiTranId
                hidHuyenHoKhau.Text = _jth.HuyenId
                hidXaThiTranHoKhau.Text = _jth.XaThiTranId

            End If
            'tam trú
            Dim _jtr = (From q In _data.CertifiedHouseholdAddresses Where q.CertifiedID = hidID.Value And q.TypeId = LoaiDiaChi.TamTru Select q).FirstOrDefault
            If Not _jtr Is Nothing Then
                txtDiaChiHienTai.Text = _jtr.Adress
                ddlTinhHienTai.SelectedValue = _jtr.TinhId

                Dim _Huyen = (From q In _data.Provinces Where q.ParentId = _jtr.TinhId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlHuyenHienTai
                    .DataSource = _Huyen
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlHuyenHienTai.SelectedValue = _jtr.HuyenId


                Dim _PhuongXa = (From q In _data.Provinces Where q.ParentId = _jtr.HuyenId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlXaThiTranHienTai
                    .DataSource = _PhuongXa
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlXaThiTranHienTai.SelectedValue = _jtr.XaThiTranId

                hidHuyenHienTai.Text = _jtr.HuyenId
                hidXaThiTranHienTai.Text = _jtr.XaThiTranId

            End If
            'đia điểm kinh doanh
            Dim _jdd = (From q In _data.CertifiedHouseholdAddresses Where q.CertifiedID = hidID.Value And q.TypeId = LoaiDiaChi.DiaDiemKinhDoanh Select q).FirstOrDefault
            If Not _jdd Is Nothing Then
                txtBiz_HeadOffice.Text = _jdd.Adress
                ddlTinhDiaDiemKD.SelectedValue = _jdd.TinhId

                Dim _Huyen = (From q In _data.Provinces Where q.ParentId = _jdd.TinhId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlHuyenDiaDiemKD
                    .DataSource = _Huyen
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlHuyenDiaDiemKD.SelectedValue = _jdd.HuyenId


                Dim _PhuongXa = (From q In _data.Provinces Where q.ParentId = _jdd.HuyenId Order By q.Title Select q.Id, q.Code, q.Title).ToList
                With ddlXaThiTranDiaDiemKD
                    .DataSource = _PhuongXa
                    .DataValueField = "Id"
                    .DataTextField = "Title"
                    .DataBind()
                    .Items.Add(New ListItem("--- Chọn ---", 0))
                End With
                ddlXaThiTranDiaDiemKD.SelectedValue = _jdd.XaThiTranId


                hidHuyenDiaDiemKD.Text = _jdd.HuyenId
                hidXaThiTranDiaDiemKD.Text = _jdd.XaThiTranId
            End If



            'kiem tra xem giay la Cap moi hay Cap doi
            If Not p.ParentId Is Nothing AndAlso p.ParentId <> p.CertifiedID Then
                Dim lstLoaiThayDoi = (From q In _data.ChangingTypes Order By q.Name Select q).ToList
                DivChanging.Visible = True
                Dim strKey_Name_CT() As String = {"Id", "Code", "Name"}
                With grdShowLoaiThayDoi
                    .DataKeyNames = strKey_Name_CT
                    .DataSource = lstLoaiThayDoi
                    .DataBind()
                End With
            Else
                DivChanging.Visible = False

            End If


        End Using
    End Sub
#End Region
#Region "Sub and Function"
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1)
        Using data As New VSEntities
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            'lấy Mã GCN từ Id hồ sơ

            Dim _ParentId As Integer = 0
            Dim temp = (From q In data.CertifiedHouseholds
                           Where q.CertifiedID = hidID.Value Select q.ParentId).FirstOrDefault
            If Not temp Is Nothing Then
                _ParentId = temp
            End If

            Dim p As List(Of CertifiedHousehold)
            If _ParentId <> 0 Then
                p = (From q In data.CertifiedHouseholds
                     Where (q.ParentId = _ParentId Or q.CertifiedID = _ParentId) Order By q.CertifiedID Select q).ToList
            Else
                p = (From q In data.CertifiedHouseholds
                     Where q.CertifiedID = hidID.Value Or q.ParentId = hidID.Value Order By q.CertifiedID Select q).ToList
            End If

            Dim strKey_Name() As String = {"CertifiedID", "CertifiedIdOld", "Biz_VietName", "ParentId"}
            'Tong so ban ghi
            hidCount.Value = p.Count
            Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
            lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        Using _data As New VSEntities
            If e.Row.RowIndex >= 0 Then
                Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
                lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
                Dim hplBiz_VietName As HyperLink = CType(e.Row.FindControl("hplBiz_VietName"), HyperLink)
                hplBiz_VietName.NavigateUrl = "../../Page/CertifiedHousehold/Detail.aspx?Certifiedid=" & grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString
                hplBiz_VietName.Text = grdShow.DataKeys(e.Row.RowIndex)("Biz_VietName").ToString

                Dim hplParentId As HyperLink = CType(e.Row.FindControl("hplParentId"), HyperLink)
                'lấy danh sách cấp đổi lần thứ mấy show ra list xem thành phần hồ sơ trong detail
                If grdShow.DataKeys(e.Row.RowIndex)("ParentId") <> grdShow.DataKeys(e.Row.RowIndex)("CertifiedID") Then
                    Dim _parId As Integer = grdShow.DataKeys(e.Row.RowIndex)("ParentId")
                    Dim _cerID As Integer = grdShow.DataKeys(e.Row.RowIndex)("CertifiedID")
                    Dim _CerList = (From q In _data.CertifiedHouseholds Where q.CertifiedID = _cerID Select q.CertifiedUp_Time).FirstOrDefault
                    'Dim _i As Integer
                    'Dim a As Integer
                    'Dim _cerMaster As Integer
                    'For _i = 1 To _CerList.Count
                    '    a = _CerList(_i)
                    '    _cerMaster = (From q In _data.CertifiedHouseholds Where q.CertifiedID = _cerID Select q.CertifiedID).FirstOrDefault
                    '    _i = _i + 1
                    'Next
                    'If a = _cerMaster Then
                    '    hplParentId.Text = "Cấp đổi lần " & (_i)
                    'End If
                    hplParentId.Text = "Cấp đổi lần " & _CerList
                Else
                    hplParentId.Text = "Đăng ký lần đầu"
                End If
                hplParentId.NavigateUrl = "../../Page/QuanLyHoSo/Detail.aspx?Certifiedid=" & grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString

            End If
        End Using
    End Sub

    Protected Sub grdShowTp_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowTp.RowDataBound

        Using _data As New VSEntities
            If e.Row.RowIndex >= 0 Then
                Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
                lblSTT.Text = e.Row.RowIndex + 1
                'lay thanh phan ho so 
                Dim _j = (From q In _data.CertifiedHouseholdThanhPhanHoSoes Where q.CertifiedID = hidID.Value Select q.ThanhPhanHoSoId).ToArray
                If Not _j Is Nothing Then
                    Dim chkItem As CheckBox = e.Row.FindControl("chkItem")
                    'kiem tra id cua item co trong tphoso hay ko? neu co thi check ko thi uncheck
                    Dim curId As Integer = grdShowTp.DataKeys(e.Row.RowIndex)("Id")
                    If Not grdShowTp.DataKeys(e.Row.RowIndex)("Id") Is Nothing AndAlso _j.Contains(curId) Then
                        chkItem.Checked = 1
                    End If

                End If
            End If
        End Using
    End Sub
    Protected Sub grdShowLoaiThayDoi_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShowLoaiThayDoi.RowDataBound


        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = e.Row.RowIndex + 1
            'Loai thay doi
            Using _data As New VSEntities
                Dim _j = (From q In _data.CertifiedHouseholdChangingTypes Where q.CertifiedID = hidID.Value Select q.ChangingTypeiId).ToArray
                If Not _j Is Nothing Then
                    Dim chkItem As CheckBox = e.Row.FindControl("chkItem")
                    'kiem tra id cua item co trong tphoso hay ko? neu co thi check ko thi uncheck
                    Dim curId As Integer = grdShowLoaiThayDoi.DataKeys(e.Row.RowIndex)("Id")
                    If Not grdShowLoaiThayDoi.DataKeys(e.Row.RowIndex)("Id") Is Nothing AndAlso _j.Contains(curId) Then
                        chkItem.Checked = 1
                    End If

                End If
            End Using
        End If


    End Sub

#End Region
End Class
